﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using Museum.DBAccess;

public partial class ManageReviews : System.Web.UI.Page
{
    //int reviewId = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (ViewState["FirstTime"] != null)
            {
                ViewState["FirstTime"] = null;
                ViewState["SecondTime"] = "1";
            }
            else if (ViewState["SecondTime"] == null)
                ViewState["FirstTime"] = "1";

            if (!string.IsNullOrEmpty(ddlGame.SelectedValue))
                ViewState["FirstGameId"] = ddlGame.SelectedValue;

            BindGridData();
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    /// <summary>
    /// Delete records from database
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void DeleteRecord(object sender, GridViewDeleteEventArgs e)
    {
        int reviewId = Convert.ToInt32(GridViewReviews.DataKeys[e.RowIndex].Value);
        int result = 0;
        try
        {
            result = DBAccess.DeleteReviewData(reviewId);
            Alert.ShowMessage("Record Deleted successfully.");
            //Refresh the data
            BindGridData();
        }
        catch (Exception ex)
        {
            lblMessage.Text = ex.Message;
        }
        finally
        {
        }
    }

    /// <summary>
    /// Binds my grid data.
    /// </summary>
    private void BindGridData()
    {
        DataTable dtReviews = new DataTable();
        DataTable dtGames = new DataTable();
        try
        {
            if (ViewState["FirstTime"] != null)
            {
                if (ddlOption.SelectedValue.Equals("0"))
                {
                    BindMuseumData();
                }
            }

            if (ViewState["MuseumId"] != null)
            {
                dtGames = DBAccess.GetGameByOption(Convert.ToInt32(ddlOption.SelectedValue), Convert.ToInt32(ddlMuseum.SelectedValue));

                ddlGame.DataSource = dtGames;
                ddlGame.DataBind();
            }

            if (ViewState["FirstGameId"] != null)
            {
                ddlGame.SelectedValue = ViewState["FirstGameId"].ToString();
                dtReviews = DBAccess.GetReviews(Convert.ToInt32(ViewState["FirstGameId"]));
            }
            else
            {
                dtReviews = DBAccess.GetReviews(Convert.ToInt32(ddlGame.SelectedValue));
            }

            //assign DataTable to GridView
            GridViewReviews.DataSource = dtReviews;
            ViewState["gvData"] = dtReviews;
            GridViewReviews.DataBind();
        }
        catch (Exception ex)
        {
            //lblMessage.Text = ex.Message;
        }
        finally
        {
        }
    }

    private void BindMuseumData()
    {
        DataTable dtMuseums = null;
        DataTable dtGames = null;
        try
        {
            ddlMuseum.DataTextField = "MuseumName";
            ddlMuseum.DataValueField = "MuseumId";

          //  if (ViewState["FirstTime"] != null)
            //{
                dtMuseums = DBAccess.GetMuseumByCity("");
                if (dtMuseums != null && dtMuseums.Rows.Count > 0)
                {
                    ddlMuseum.DataSource = dtMuseums;
                    ddlMuseum.DataBind();
                }
            //}

            dtGames = DBAccess.GetGameByMuseum(Convert.ToInt32(ddlMuseum.SelectedValue));
            if (dtMuseums != null && dtMuseums.Rows.Count > 0)
            {
                ddlGame.Items.Clear();
                ddlGame.SelectedValue = null;

                ddlGame.DataSource = dtGames;
                ddlGame.DataBind();
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    private void BindSchoolData()
    {
        DataTable dtSchools = null;
        DataTable dtGames = null;
        try
        {
            ddlMuseum.DataTextField = "SchoolName";
            ddlMuseum.DataValueField = "SchoolId";

            dtSchools = DBAccess.GetAllSchools();

            if (dtSchools != null && dtSchools.Rows.Count > 0)
            {
                ddlMuseum.DataSource = dtSchools;
                ddlMuseum.DataBind();
            }

            dtGames = DBAccess.GetGamesBySchool(Convert.ToInt32(ddlMuseum.SelectedValue));

            if (dtGames != null && dtGames.Rows.Count > 0)
            {
                ddlGame.Items.Clear();
                ddlGame.SelectedValue = null;

                ddlGame.DataSource = dtGames;
                ddlGame.DataBind();
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    protected void ddlMuseum_SelectedIndexChanged(object sender, EventArgs e)
    {
        ViewState["MuseumId"] = ddlMuseum.SelectedValue;
        BindGridData();
    }

    protected void ddlGame_SelectedIndexChanged(object sender, EventArgs e)
    {
        ViewState["GameId"] = ddlGame.SelectedValue;
        BindGridData();
    }

    protected void ddlOption_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddlOption.SelectedValue.Equals("0"))
        {            
            BindMuseumData();
        }
        else
        {
            BindSchoolData();
        }

        BindGridData();
    }

    private string ConvertSortDirectionToSql(SortDirection sortDirection)
    {
        string newSortDirection = String.Empty;

        switch (sortDirection)
        {
            case SortDirection.Ascending:
                newSortDirection = "ASC";
                break;

            case SortDirection.Descending:
                newSortDirection = "DESC";
                break;
        }

        return newSortDirection;
    }

    protected void gridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridViewReviews.PageIndex = e.NewPageIndex;
        if (ViewState["gvData"] != null)
            GridViewReviews.DataSource = (DataTable)ViewState["gvData"];
        GridViewReviews.DataBind();
    }

    protected void gridView_Sorting(object sender, GridViewSortEventArgs e)
    {
        DataTable dataTable = GridViewReviews.DataSource as DataTable;

        if (dataTable != null)
        {
            DataView dataView = new DataView(dataTable);
            dataView.Sort = e.SortExpression + " " + ConvertSortDirectionToSql(e.SortDirection);

            GridViewReviews.DataSource = dataView;
            GridViewReviews.DataBind();
        }
    }
}
